package com.hty.qrcode;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends Activity {
    EditText editText;
    ImageView imageView;
    Switch switch_logo;
    Button button_create, button_save, button_share;
    ImageButton imageButton_clear;
    InputMethodManager IMM;
    Bitmap bitmap_qrcode = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IMM = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        editText = (EditText) findViewById(R.id.editText);
        imageButton_clear = (ImageButton) findViewById(R.id.imageButton_clear);
        imageButton_clear.setOnClickListener(new MyOnClickListener());
        switch_logo = (Switch) findViewById(R.id.switch_logo);
        switch_logo.setOnClickListener(new MyOnClickListener());
        button_create = (Button) findViewById(R.id.button_create);
        button_create.setOnClickListener(new MyOnClickListener());
        button_save = (Button) findViewById(R.id.button_save);
        button_save.setOnClickListener(new MyOnClickListener());
        button_share = (Button) findViewById(R.id.button_share);
        button_share.setOnClickListener(new MyOnClickListener());
        imageView = (ImageView) findViewById(R.id.imageView);

        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        Log.e(Thread.currentThread().getStackTrace()[2] + "", action + ", " + type);
        if (action.equals(Intent.ACTION_SEND) && type.equals("text/plain")) {
            editText.setText(intent.getStringExtra(Intent.EXTRA_TEXT));
            button_create.performClick();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "关于");
        menu.add(0, 1, 1, "更新日志");
        menu.add(0, 2, 2, "退出");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int item_id = item.getItemId();
        switch (item_id) {
            case 0:
                new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("二维码生成分享 V1.1")
                        .setMessage("文字生成二维码，文字分享到二维码。\n作者：海天鹰\nQQ：84429027\n基于源码：https://gitee.com/wd23/qrcodeDemo\n二维码库：zxing")
                        .setPositiveButton("确定", null).show();
                break;
            case 1:
                new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("更新日志")
                        .setMessage("V1.1 (2021-11-21)\n1.使用Switch控件控制是否显示LOGO，用1张图片代替2张图片。\n2.增加保存和分享二维码。\n\nV1.0 (2021-11-19)\n1.优化二维码源码，增加分享接口。")
                        .setPositiveButton("确定", null).show();
                break;
            case 2:
                finish();
                break;
        }
        return true;
    }

    class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.imageButton_clear:
                    editText.setText("");
                    break;
                case R.id.button_create:
                case R.id.switch_logo:
                    IMM.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                    if (editText.getText().toString().equals("")){
                        Toast.makeText(MainActivity.this, "请输入要生成二维码的内容", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Bitmap bitmap_logo = null;
                    if (switch_logo.isChecked()) {
                        bitmap_logo = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
                    }
                    //生成二维码
                    bitmap_qrcode  = EncodingUtils.createQRCode(editText.getText().toString(), 500, 500, bitmap_logo);
                    imageView.setImageBitmap(bitmap_qrcode);
                    break;
                case R.id.button_save:
                    if (bitmap_qrcode != null)
                        Uri.parse(MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), bitmap_qrcode, "QRCode", "QRCode made by HTY."));
                    break;
                case R.id.button_share:
                    if (bitmap_qrcode != null) {
                        Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), bitmap_qrcode, "QRCode", "QRCode made by HTY.")); //保存bitmap到存储，插入数据到媒体库，获取图片的uri
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_SEND);
                        intent.putExtra(Intent.EXTRA_STREAM, uri);
                        intent.setType("image/*");
                        startActivity(Intent.createChooser(intent, "分享到"));
                    }
                    break;
            }
        }
    }

}